package main;

import java.util.LinkedList;
import java.util.Queue;

public class MovingAverage {
    private final Queue<Integer> nums;
    private final int capacity;
    private int sum;
    public MovingAverage(int size){
        nums = new LinkedList<>();
        capacity = size;
    }
    public double next(int val){
        nums.offer(val);
        sum += val;
        if (nums.size() > capacity){
            sum -= nums.poll();
        }
        return (double) sum / nums.size();
    }

    public static void main(String[] args) {
        MovingAverage movingAverage = new MovingAverage(3);
        movingAverage.next(1);
        movingAverage.next(2);
        System.out.println(movingAverage.next(3));
        System.out.println(movingAverage.next(4));
    }
}
